package adminDownload

import (
	"server/models"
	serviceDownload "server/service/download"
	"server/utils"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func Get(c *gin.Context) {
	page, _ := strconv.ParseInt(c.Query("page"), 10, 32)
	perPage, _ := strconv.ParseInt(c.Query("per_page"), 10, 32)
	name := c.Query("name")

	res, errRes := serviceDownload.GetList(page, perPage, name)
	if errRes != nil {
		c.AbortWithError(500, errRes)
		return
	}
	// 正常返回数据
	c.JSON(
		200,
		utils.DealResponse(utils.Response{
			"data": gin.H{
				"list": res.List,
				"page": gin.H{
					"page":     page,
					"per_page": perPage,
					"total":    res.Count,
				},
			},
		}),
	)
}

func Create(c *gin.Context) {
	var postData struct {
		Name string `json:"name"`
	}
	c.BindJSON(&postData)
	var doc models.AdminDownload
	doc.Name = "张三的报名资料2"
	doc.UserID = 2
	doc.Url = "https://www.baidu.com"
	doc.Status = 2
	doc.CreatedTime = uint64(time.Now().Unix())
	doc.UpdatedTime = uint64(time.Now().Unix())
	res, errRes := serviceDownload.Create(doc)
	if errRes != nil {
		c.AbortWithError(500, errRes)
		return
	}

	c.JSON(
		200,
		utils.DealResponse(utils.Response{
			"data": res,
		}),
	)
}
